import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart' ;
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mfkb/config/Config.dart';
import 'package:mfkb/pages/modules/CustomRoute.dart';
import 'package:mfkb/pages/modules/MyDialog.dart';
import 'package:mfkb/pages/modules/Nothing.dart';
import 'package:mfkb/pages/school/RelationDetail.dart';
import 'package:mfkb/pages/school/plugins/ImageSwiper.dart';
import 'package:mfkb/plugin/MyBehavior.dart';
import 'package:mfkb/service/DynamicService.dart';
import 'package:toast/toast.dart';

class MyDynamic extends StatefulWidget {
  @override
  _MyDynamicState createState() => _MyDynamicState();
}

class _MyDynamicState extends State<MyDynamic> {

  List _list ; 
  var swiper_list = null ;
  int swiper_index = 0 ;

  @override
  void initState() {
    super.initState();
    DynamicService().myDynamic().then((value){
      if(value["code"] == 200){
        _list = value["data"] ;
        setState(() {});
      }else{
        Toast.show(value["msg"], context) ;
      }
    }) ;
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark ,
      child: Scaffold(
        body: Container(
          color: Colors.white ,
          child: Column(
            children: <Widget>[
              Container(
                height: MediaQuery.of(context).padding.top ,
                color: Colors.white ,
              ) ,
              Container(
                height: 45,
                decoration: BoxDecoration(
                  color:Colors.white ,
                  border: Border(
                    bottom: BorderSide(
                      color:Color.fromARGB(255, 246, 246, 246) ,
                      width: ScreenUtil().setHeight(1)
                    )
                  )
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start ,              
                  children: <Widget>[
                    Container(
                      width: ScreenUtil().setWidth(30),
                      height: ScreenUtil().setWidth(17),
                      margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                      child: GestureDetector(
                        child: Container(
                          width: ScreenUtil().setWidth(17),
                          margin: EdgeInsets.only(right:ScreenUtil().setWidth(13)),
                          child: Image.asset("assets/images/back.png" , fit: BoxFit.fill,),
                        ),
                        onTap: (){
                          Navigator.of(context).pop() ;
                        },
                      ),
                    ) ,
                    Expanded(
                      child: Center(
                        child: Text("我的发布" , style: TextStyle(
                          fontSize: ScreenUtil().setSp(18) ,
                          decoration: TextDecoration.none ,
                          color: Color.fromARGB(255, 19, 28, 53)
                        )),
                      ),
                    ) ,
                    Container(
                      width: ScreenUtil().setWidth(30),
                      height: ScreenUtil().setWidth(17),
                      margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                    )
                  ],
                ),
              ) ,
              Expanded(
                child: Container(
                  color: Colors.white,
                  width: ScreenUtil().setWidth(375),
                  child: _list == null || _list.length == 0 ? Container(
                    color: Colors.white ,
                    width: ScreenUtil().setWidth(375),
                    child: Nothing(),
                  ) : ScrollConfiguration(
                    behavior: MyBehavior() ,
                    child: ListView(
                      padding: EdgeInsets.all(0),
                      children: List.generate(_list.length, (index){
                        return GestureDetector(
                          child: Container(
                            margin: EdgeInsets.only(left:ScreenUtil().setWidth(15) , right:ScreenUtil().setWidth(14)),
                            color: Colors.white ,
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start ,
                              children: <Widget>[
                                Container(
                                  height: ScreenUtil().setWidth(40),
                                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(14)),
                                  child: Row(
                                    children: <Widget>[
                                      Container(
                                        width: ScreenUtil().setWidth(40) ,
                                        height: ScreenUtil().setWidth(40) ,
                                        child: ClipRRect(
                                          borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setWidth(40))),
                                          child: CachedNetworkImage(
                                            imageUrl: Config().getImgHost() + _list[index]["icon"] ,
                                            placeholder: (contex , url){
                                              return Container(
                                                color:Color.fromARGB(255, 246, 245, 248) ,
                                                child: Center(
                                                  child: Image.asset("assets/images/imageloading.png"),
                                                ) ,
                                              ) ;
                                            },
                                          ),
                                        )
                                      ) ,
                                      Expanded(
                                        child: Container(
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            children: <Widget>[
                                              Container(
                                                height: ScreenUtil().setWidth(25),
                                                child: Row(
                                                  children: <Widget>[
                                                    Expanded(
                                                      child: Container(
                                                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(10)),
                                                        child: Text(_list[index]["nickname"] , style: TextStyle(
                                                          fontSize: ScreenUtil().setSp(16) ,
                                                          color: Color.fromARGB(255, 19, 28, 53) ,
                                                          fontWeight: FontWeight.bold ,
                                                          fontFamily: "PingFang-SC-Bold,PingFang-SC"
                                                        ),),
                                                      ),
                                                    ) ,
                                                    GestureDetector(
                                                      child: Container(
                                                        width: ScreenUtil().setWidth(80),
                                                        color: Color.fromARGB(0, 0, 0, 0),
                                                        child: Text(
                                                          "删除" , style: TextStyle(
                                                            fontSize: ScreenUtil().setSp(12) ,
                                                            color: Color.fromARGB(255, 121, 132, 155)
                                                          ),textAlign: TextAlign.end ,
                                                        ),
                                                      ),
                                                      onTap: (){
                                                        print(_list[index]["id"]) ;
                                                        showDialog(
                                                          context: context ,
                                                          child: MyDialog(title: "确定删除" , no: (){} , yes:(){
                                                            DynamicService().delDynamic(_list[index]["id"].toString()).then((value){
                                                              if(value["code"] == 200){
                                                                Toast.show("删除成功", context) ;
                                                                _list.removeAt(index) ;
                                                                setState(() {});
                                                                Navigator.of(context).pop() ;
                                                              }else{
                                                                Toast.show("删除失败", context) ;
                                                              }
                                                            }) ;
                                                          })
                                                        ) ;
                                                      },
                                                    )
                                                  ],
                                                ),
                                              ) ,
                                              Container(
                                                height: ScreenUtil().setWidth(15),
                                                margin: EdgeInsets.only(left:ScreenUtil().setWidth(10)),
                                                child: Text(_list[index]["school"] , textAlign: TextAlign.start,style: TextStyle(
                                                  fontSize: ScreenUtil().setSp(12) ,
                                                  height: ScreenUtil().setWidth(13) / ScreenUtil().setSp(12),
                                                  color:Color.fromARGB(255, 121, 132, 155)
                                                ),),
                                              )
                                            ],
                                          ),
                                        ),
                                      ) ,
                                    ],
                                  ),
                                ) ,
                                Container(
                                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(14)),
                                  child: Text(_list[index]["content"] , style: TextStyle(
                                    fontSize: ScreenUtil().setSp(14) ,
                                    fontWeight: FontWeight.w500 
                                  ),),
                                ) ,
                                Visibility(
                                  visible: _list[index]["images"] != null && _list[index]["images"].length != 0,
                                  child: Container(
                                    margin: EdgeInsets.only(top:ScreenUtil().setHeight(_list[index]["images"] == null || _list[index]["images"].length == 0 ? 0 : 14)),
                                    child: Row(
                                      children: List.generate(_list[index]["images"] == null ? 0 : _list[index]["images"].length, (i){
                                        return  Container(
                                          width: ScreenUtil().setWidth(110),
                                          height: ScreenUtil().setWidth(110),
                                          decoration: BoxDecoration(
                                            color: Colors.black ,
                                            borderRadius: BorderRadius.all(Radius.circular(8)),
                                          ),
                                          margin: EdgeInsets.only(left:ScreenUtil().setWidth(i == 0 ? 0 : 8)),
                                          child: GestureDetector(
                                            child: ClipRRect(
                                              borderRadius: BorderRadius.all(Radius.circular(8)),
                                              child:CachedNetworkImage(
                                                key: _list[index]["images"][i]["id"] ,
                                                imageUrl: Config().getHost()+_list[index]["images"][i]["url"],
                                                fit: BoxFit.cover ,
                                                placeholder: (contex , url){
                                                  return Container(
                                                    color:Color.fromARGB(255, 246, 245, 248) ,
                                                    child: Center(
                                                      child: Image.asset("assets/images/imageloading.png"),
                                                    ) ,
                                                  ) ;
                                                },
                                              )
                                            ) ,
                                            onTap: (){  
                                              setState(() { 
                                                swiper_list = _list[index]["images"] ;
                                                //swiper_show = true ;
                                                swiper_index = i ;
                                                Navigator.of(context).push(CustomRoute(ImageShowServerWidget(
                                                  initialIndex: swiper_index,
                                                  photoList: swiper_list,
                                                ))) ;

                                              });
                                            },
                                          )
                                        ) ;
                                      }),
                                    ),
                                  ),
                                ) ,
                                Container(
                                  height: ScreenUtil().setWidth(20),
                                  margin: EdgeInsets.only(bottom:ScreenUtil().setHeight(22) , top:ScreenUtil().setHeight(22)),
                                  child: Row(
                                    children: <Widget>[
                                      Container(
                                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(144)),
                                        width: ScreenUtil().setWidth(80),
                                        child:GestureDetector(
                                          child: Row(
                                            children: <Widget>[
                                              Container(
                                                width: ScreenUtil().setWidth(20),
                                                height: ScreenUtil().setWidth(20),
                                                child: Image.asset("assets/images/talk.png"),
                                              ) ,
                                              Expanded(
                                                child: Container(
                                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(5)),
                                                  child: Text(_list[index]["talk"].toString() , style: TextStyle(
                                                    fontSize: ScreenUtil().setSp(14) ,
                                                    color: Color.fromARGB(255, 121, 132, 155)
                                                  ),),
                                                )
                                              )
                                            ],
                                          ),
                                          onTap: (){
                                            Navigator.of(context).push(CustomRoute(RelationDetail(id: _list[index]["id"],))) ;
                                          },
                                        )
                                      ) ,
                                      Container(
                                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(50)),
                                        width: ScreenUtil().setWidth(70),
                                        child:GestureDetector(
                                          child: Row(
                                            children: <Widget>[
                                              Container(
                                                width: ScreenUtil().setWidth(20),
                                                height: ScreenUtil().setWidth(20),
                                                child: _list[index]["gid"] == null ? Image.asset("assets/images/up.png") : Image.asset("assets/images/up_active.png"),
                                              ) ,
                                              Expanded(
                                                child: Container(
                                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(5)),
                                                  child: Text(_list[index]["up"].toString() , style: TextStyle(
                                                    fontSize: ScreenUtil().setSp(14) ,
                                                    color: Color.fromARGB(255, 121, 132, 155)
                                                  ),),
                                                )
                                              )
                                            ],
                                          ),
                                          onTap: () {
                                            if(_list[index]["gid"] == null){
                                              DynamicService().up(_list[index]["id"] , 1).then((value){
                                                if(value["code"] == 200){
                                                  setState(() {
                                                    _list[index]["gid"] = 1 ;
                                                    _list[index]["up"]++ ;
                                                  });
                                                }  
                                              }) ;
                                            }else{
                                              DynamicService().up(_list[index]["id"] , 0).then((value){
                                                if(value["code"] == 200){
                                                  setState(() {
                                                    _list[index]["gid"] = null ;
                                                    _list[index]["up"]-- ;
                                                  });
                                                }  
                                              }) ;
                                            }
                                          },
                                        )
                                      )
                                    ],
                                  ),
                                ) ,
                                Container(
                                  height: ScreenUtil().setHeight(1),
                                  color: Color.fromARGB(255, 236, 237, 241),
                                )
                              ],
                            ),
                          ) ,
                          onTap: (){
                            Navigator.of(context).push(CustomRoute(RelationDetail(id:_list[index]["id"]))) ;
                          },
                        ) ;
                      }),
                    ),
                  )
                )
              )
            ]
          )
        )
      )
    );
  }
}